package com.avertymaroc.users;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.avertymaroc.app.MainActivity;
import com.avertymaroc.app.R;
import com.avertymaroc.beans.MyException;
import com.avertymaroc.beans.User;
import com.avertymaroc.library.AlertDialogue;
import com.avertymaroc.library.C;
import com.avertymaroc.library.HttpManager;
import com.avertymaroc.library.SessionManager;

public class Profil extends SherlockFragmentActivity {
	
	private static final String KEY_NEWMDP		 = "mpNew";

	

	private EditText prenom, nom, email, tel, adresse, ance_mdp, mdp;
	private Spinner ville, profession, annee;
	private Button modifier ;
	private String motDePasse;
	private ImageButton logOut , homePage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(MainActivity.THEME);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mon_profil);
		init();

	}

	

	
	private void init() {

		prenom 		= (EditText) findViewById(R.id.profil_prenom);
		nom 		= (EditText) findViewById(R.id.profil_nom);
		email 		= (EditText) findViewById(R.id.profil_email);
		tel			= (EditText) findViewById(R.id.profil_tel);
		adresse 	= (EditText) findViewById(R.id.profil_adresse);
		ance_mdp 	= (EditText) findViewById(R.id.profil_anc_mdp);
		mdp 		= (EditText) findViewById(R.id.profil_nov_mdp);
		ville 		= (Spinner) findViewById(R.id.profil_ville);
		profession 	= (Spinner) findViewById(R.id.profil_profession);
		annee 		= (Spinner) findViewById(R.id.profil_annee);
		modifier 	= (Button) findViewById(R.id.modifier); 
		
		Listener l = new Listener();
		modifier.setOnClickListener(l);
		initActionBarButtons();
		
		List<String> anne = new ArrayList<String>();
		int current_year = Calendar.getInstance().get(Calendar.YEAR);
		for (int i = current_year - 14; i > current_year - 94; i--)
			anne.add("" + i);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, anne);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		annee.setAdapter(adapter);

		Intent i = getIntent();
		prenom.setText(i.getStringExtra(User.KEY_PRENOM));
		nom.setText(i.getStringExtra(User.KEY_NOM));
		email.setText(i.getStringExtra(User.KEY_EMAIL ));
		tel.setText(i.getStringExtra(User.KEY_TEL));
		adresse.setText(i.getStringExtra(User.KEY_ADRESSE));
		ville.setSelection(i.getIntExtra(User.KEY_VILLE, 1) - 1 , true);
		profession.setSelection(i.getIntExtra(User.KEY_PROFESSION, 1) - 1, true);
		
		int fisrtYear = Integer.parseInt((String)annee.getItemAtPosition(0));
		
		annee.setSelection(fisrtYear - i.getIntExtra(User.KEY_ANNE, fisrtYear) , true);
		motDePasse = new SessionManager(getApplicationContext()).getUserDetails().getMdp();
		email.setEnabled(false);
			
		
	}
	
 class Listener implements OnClickListener {

	@Override
	public void onClick(View v) {
		
		try{
			validation();
			HashMap<String, String> p = new HashMap<String, String>();
			
			User u = new SessionManager(getApplicationContext()).getUserDetails();
				p.put(User.KEY_PRENOM, prenom.getText().toString());
			
				p.put(User.KEY_NOM, nom.getText().toString());
			
				p.put(User.KEY_PROFESSION, ""+(profession.getSelectedItemPosition() + 1));
				p.put(User.KEY_ANNE, (String ) annee.getSelectedItem());
				p.put(User.KEY_VILLE, ""+(ville.getSelectedItemPosition() + 1));
				p.put(User.KEY_TEL, tel.getText().toString());
				p.put(User.KEY_ADRESSE,adresse.getText().toString());
				p.put(KEY_NEWMDP ,mdp.getText().toString() );
				p.put(User.KEY_ID, new SessionManager(Profil.this).getUserDetails().getId()+"");
				
			HttpManager modifProfil = new HttpManager(Profil.this) {
				
				@Override
				protected void onPostExecute(String file_url) {
					getpDialog().dismiss();
					JSONObject res = getResult();
					try {
						int succes = res.getInt(C.TAG_SUCCESS);
						if(succes == 1){
						new SessionManager(getApplicationContext()).getUserDetails().setMdp(mdp.getText().toString());
							AlertDialogue.afficher(Profil.this, R.string.profil_succes_update , R.string.opertation_reussie ,R.string.btn_ok);
						}else{
							String errors = res.getString("errors");
							//AlertDialogue.afficher(Profil.this, errors);

							AlertDialogue.afficher(Profil.this, R.string.profil_failure_update);
						}
					} catch (JSONException e) {
						Toast.makeText(Profil.this, e.getMessage(), Toast.LENGTH_LONG).show();
					}
					
				}
			};

			modifProfil.setUrl( C.site_url("mobile/profil/modifier") )
						.setParams(p)
						.execute();
			
			
			
		}catch(MyException e){
			AlertDialogue.afficher(Profil.this, e.getMessage());
		}
		
		
	}
 }
 
 private void validation() throws MyException{
	 String oldMotDePass = ance_mdp.getText().toString();
	 
	 	if(!oldMotDePass.equals("") ){
	 			if( !oldMotDePass.equals(motDePasse)  )
	 				throw new MyException(getResources().getString(R.string.profil_nov_mdp_invalide));
	 			else
	 				if(mdp.getText().toString().length() == 0){
	 					throw new MyException(getResources().getString(R.string.profil_nov_mpd_vide));
	 				}else
	 					if(mdp.getText().toString().length() < 0)
		 					throw new MyException(getResources().getString(R.string.err_pass_court));
	 	}
	 
 }
 
 private void initActionBarButtons(){
	 
	 logOut = (ImageButton) findViewById(R.id.ab_right_button);
	 homePage = (ImageButton) findViewById(R.id.ab_left_button);
	 
	 logOut.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			new SessionManager(getApplicationContext()).logoutUser();
			
		}
	});
	 homePage.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		
			finish();
			
		}
	});
	 
 }
    

}
